/*
 * Copyright (C) 2017 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.umeox.watch.moto.launcher.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.recyclerview.widget.RecyclerView;

import com.umeox.moto.common.clicklistener.OnNoDoubleClickListener;
import com.umeox.watch.moto.launcher.R;

public class WatchFaceStyleAdapter extends RecyclerView.Adapter<WatchFaceStyleAdapter.Holder> {

    private final LayoutInflater mInflater;
    private int[] iconData;
    private OnClickListener mOnClickListener;

    private Context mContext;

    public WatchFaceStyleAdapter(Context context, int[] items) {
        this.iconData = items;
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View itemView = mInflater.inflate(R.layout.item_watch_face_style, parent, false);
        return new Holder(itemView);
    }


    @Override
    public void onBindViewHolder(Holder holder, @SuppressLint("RecyclerView") int position) {
        if (iconData == null || iconData.length == 0) {
            return;
        }

        holder.bind(iconData[position]);
        holder.itemView.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (mOnClickListener != null) {
                    mOnClickListener.onPositionClick(position);
                }
            }
        });
    }


    @Override
    public int getItemCount() {
        return iconData.length;
    }


    static class Holder extends RecyclerView.ViewHolder {
        ImageView mImageView;

        public Holder(final View itemView) {
            super(itemView);

            mImageView = itemView.findViewById(R.id.iv_icon);
        }

        public void bind(int resId) {
            mImageView.setImageResource(resId);
        }
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        mOnClickListener = onClickListener;
    }

    public interface OnClickListener {
        void onPositionClick(int position);
    }
}
